package kadledav.adventure.framework.builder.name;

import kadledav.adventure.framework.model.world.impl.LocationBasic;
import kadledav.adventure.framework.model.world.impl.PassageBasic;

class SimpleNameBuilderPassage {

	private void throwGeneralAbstractBuildConnectionException() {
		throw new RuntimeException("The locations cannot be connected");
	}

	public void createConnection(LocationBasic location1, LocationBasic location2, final String passageName) {
		createConnectionOneDir(location1, location2, passageName);
		createConnectionOneDir(location2, location1, passageName);
	}

	public void createConnectionWithLock(LocationBasic location1, LocationBasic location2, final String passageName,String newPassageName, final String unlockWithItem) {
		SimpleNameLockedPassage from1to2 = createConnectionOneDirWithLock(location1, location2, passageName, newPassageName, unlockWithItem);
		SimpleNameLockedPassage from2to1 = createConnectionOneDirWithLock(location2, location1, passageName, newPassageName, unlockWithItem);
		if (from1to2 == null || from2to1 == null) {
			throwGeneralAbstractBuildConnectionException();
		}
		from1to2.setOtherSide(from2to1);
		from2to1.setOtherSide(from1to2);
	}


	protected PassageBasic createConnectionOneDir(LocationBasic locationFrom, LocationBasic locationTo, final String passageName) {
		if (locationFrom == null || locationTo == null) {
			throwGeneralAbstractBuildConnectionException();
		}
		PassageBasic passage = new PassageBasic(passageName, "", locationTo);
		locationFrom.addPassage(passage);
		return passage;
	}

	protected SimpleNameLockedPassage createConnectionOneDirWithLock(LocationBasic locationFrom, LocationBasic locationTo, String passageName,String newPassageName, final String unlockWithItem) {
		if (locationFrom == null || locationTo == null || unlockWithItem == null) {
			throwGeneralAbstractBuildConnectionException();
		}
		SimpleNameLockedPassage lockedPassage = new SimpleNameLockedPassage(passageName, locationTo);
		lockedPassage.addUnlockItem(unlockWithItem, newPassageName);
		locationFrom.addPassage(lockedPassage);
		return lockedPassage;		
	}
}
